//获取应用实例
var Bmob = require('../../../utils/bmob.js');
var app = getApp();

Page({
  //数据
  data: {
    page:1,             //当前页
    limit:20,           //页容量
    dataList:[],        //列表数据
    isAdUnitId:false,   //是否开启广告
  },

  //页面加载
  onLoad: function (options) {
    var that=this;
    that.setData({
      adUnitIdBanner:app.globalData.config.adUnitIdBanner,
      adUnitIdBig:app.globalData.config.adUnitIdBig,
    });
    //that.getLbt();
    that.getDataList();
    tt.setNavigationBarTitle({title: app.globalData.config.applicationName});
  },

  //预览
  showImg:function(imgUrl){
    var that = this;
    console.info("imgurl = ",imgUrl);
    if(imgUrl.indexOf("http") !=-1){
      return imgUrl;
    }else{
      return app.globalData.domainName+imgUrl;
    }
  },

  /** 轮播图 */
  getLbt:function(){
    tt.request({
      url: app.globalData.domainName + "api/public/groupData/get",
      data:{gid:app.globalData.config.indexLbtGid},
      success: (res) => {
        var that=this;
        var data=res.data.data;
        data.forEach(item => {
            item.YQZMBZlbt = this.showImg(item.YQZMBZlbt);
            return item;
        });
        that.setData({
          lbtList:data,
        });

        //验证是否开启广告
        if(data.length == 3 || data.length == 5 || data.length == 6){
          app.globalData.config.isAdUnitId=true;
          that.setData({
            isAdUnitId:true,
          });
        }
      },
      fail: (res) => {
      },
    });
  },

  //下滑到底部加载
  lower(){
    console.info(11111111);
    var that = this;
    that.setData({
      page: that.data.page + 1
    });
    that.getDataList();
  },
  
  /** 数据列表 */
  getDataList:function(){
    var that = this;
    var flag = true;
    
    //发起请求
    tt.request({
      url: app.globalData.domainName + 'api/public/groupData/getPage',
      data:{ gid: app.globalData.config.indexDataListGid,
        limit:that.data.limit,
        page:that.data.page
      },
      success: (res) => {
        var data=res.data.data;
        if(data == null) return;
        data.forEach(item => {
          item.nndt_timu = this.showImg(item.nndt_timu);
          return item;
        });
        that.setData({
          dataList: that.data.dataList.concat(data),
        });
        flag = false;
      }
    });

    //加载弹窗
    if(flag == true) {
      wx.showToast({
        title: '正在加载',
        icon: 'loading',
      })
    }
  },

  /** 跳转详情 */
  jumpDetails:function (e) {
    //console.info(e);
    var that=this;
    var index = e.currentTarget.dataset.index;
    var infoData = that.data.dataList[index];
    tt.setStorageSync("infoData", infoData);
    tt.setStorageSync("infoDataList", that.data.dataList);
    tt.setStorageSync("index", index);
    tt.navigateTo({
      url: "/pages/nndt/info/info",
    });
  },

})